package handler

import (
	"ecom-admi/cache"
	"ecom-admi/models"
	"ecom-admi/pkg"
	"encoding/json"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字符不能为空",
		})
		return
	}
	var user models.User
	redis_key := "login_" + username
	errData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errData >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次，禁止登录",
		})
		return
	}
	err := models.DB.Where("username=?", username).Debug().First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误1",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误2",
		})
		return
	}
	if pkg.Md5(password) != user.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次，封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误3",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func CreateRoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	nid, _ := strconv.Atoi(c.Query("nid"))
	if rid == 0 || nid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字符不能为空",
		})
		return
	}
	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return
}
func QueryUserRole(c *gin.Context) {
	/*
		SELECT
		*
		FROM
		roles
		LEFT JOIN user_roles ON roles.id=user_roles.uid
	*/
	uid := c.Query("uid")
	var role models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id=user_roles.uid").
		Where("user_roles.uid=?", uid).
		Debug().Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": role,
	})
	return
}
func QueryRoleNode(c *gin.Context) {
	/*
		SELECT
		*
		FROM
		nodes
		LEFT JOIN role_nodes ON nodes.id=role_nodes.rid
	*/
	rid := c.Query("rid")
	var node models.Node
	models.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id=role_nodes.rid").
		Where("role_nodes.rid=?", rid).
		Debug().Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": node,
	})
	return
}
func CreateGoods(c *gin.Context) {
	good_code := c.PostForm("good_code")
	good_name := c.PostForm("good_name")
	if good_code == "" || good_name == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字符不能为空",
		})
		return
	}
	data := models.Goods{
		GoodCode: good_code,
		GoodName: good_name,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return
}
func CreateGoodsOrder(c *gin.Context) {
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	order_code := c.PostForm("order_code")
	order_name := c.PostForm("order_name")
	order_price := c.PostForm("good_name")
	if gid == 0 || order_code == "" || order_name == "" || order_price == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字符不能为空",
		})
		return
	}
	data := models.GoodOrder{
		Gid:        gid,
		OrderCode:  order_code,
		OrderName:  order_name,
		OrderPrice: order_price,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return
}
func ListGoodOrder(c *gin.Context) {
	var order []models.GoodOrder
	redis_key := "list_good_order_"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Debug().Find(&order)
		jsonData, _ := json.Marshal(&order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": order,
	})
	return
}
func DetailGoodOrder(c *gin.Context) {
	id := c.Query("id")
	var order []models.GoodOrder
	redis_key := "list_good_order_"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Where("id=?", id).Debug().Find(&order)
		jsonData, _ := json.Marshal(&order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": order,
	})
	return
}
